package com.hee.erp.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hee.erp.enums.WorkflowUserTypeEnum;
import com.hee.erp.exception.BusinessException;
import com.hee.erp.modules.entity.SysUser;
import com.hee.erp.modules.entity.WfMain;
import com.hee.erp.modules.entity.WfNode;
import com.hee.erp.modules.mapper.WfMainMapper;
import com.hee.erp.modules.service.ISysUserService;
import com.hee.erp.modules.service.IWfMainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hee.erp.modules.service.IWfNodeService;
import com.hee.erp.util.CommonUtils;
import com.hee.erp.vo.page.WorkflowPageParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author weizehong
 * @since 2022-05-15
 */
@Service
@Transactional
public class WfMainServiceImpl extends ServiceImpl<WfMainMapper, WfMain> implements IWfMainService {

    @Autowired
    IWfNodeService wfNodeService;

    @Autowired
    ISysUserService sysUserService;

    @Override
    public void saveDetail(WfMain wlMain) {
        this.saveOrUpdate(wlMain);

        //删除旧节点
        wfNodeService.removeByMainId(wlMain.getId());
        //保存新节点
        WfNode node = wlMain.getNode();
        while (node != null) {
            node.setId(null);
            node.setWfMainId(wlMain.getId());

            if(node.getUserType().equals(WorkflowUserTypeEnum.NOT_SETTING)){
                throw new BusinessException("请设置节点处理人");
            }
            wfNodeService.save(node);
            String parentId = node.getId();


            node = node.getNext();
            if (node != null) {
                node.setParentId(parentId);
            }
        }
    }

    @Override
    public WfMain getDetailById(String id) {

        WfMain entity = this.getById(id);
        WfNode node = wfNodeService.getNodeChainByMainId(id);

        entity.setNode(node);

        return entity;
    }

    @Override
    public IPage listByPage(WorkflowPageParams params) {
        IPage<WfMain> page = new Page<>();
        page.setCurrent(params.getCurrent());
        page.setSize(params.getPageSize());

        QueryWrapper<WfMain> queryWrapper = createQueryWrapper(params);
        this.page(page, queryWrapper);
        setForeignKeys(page.getRecords());
        return page;
    }

    private void setForeignKeys(List<WfMain> records) {
        if(records == null || records.size() > 0){
            for (WfMain record : records) {
                setForeignKey(record);
            }
        }
    }

    private void setForeignKey(WfMain record) {
        //createUserName
        if(StringUtils.isNotBlank(record.getCreateUser())){
            SysUser user = sysUserService.getById(record.getCreateUser());
            if(user != null){
                record.setCreateUserName(user.getUserName());
            }
        }
    }

    private QueryWrapper<WfMain> createQueryWrapper(WorkflowPageParams params) {
        QueryWrapper<WfMain> queryWrapper = new QueryWrapper<>();

        //查询条件
        WorkflowPageParams.Condition condition = params.getCondition();
        if(condition != null){
            if(StringUtils.isNotBlank(condition.getKey())){
                queryWrapper.lambda().like(WfMain::getTitle, condition.getKey());
            }
        }

        //根据前端表格进行排序
        CommonUtils.setQueryOrderCondition(queryWrapper, params.getSorter());

        return queryWrapper;
    }
}
